/*
 * @Author: your name
 * @Date: 2021-05-30 22:04:17
 * @LastEditTime: 2021-09-28 21:37:44
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \telescope720P\src\interface.cpp
 */
#include "interface.h"
#include "xlog.h"
#include "time_out.h"
Interface *Interface::instance = nullptr;
Interface::Interface()
{
}
Interface::~Interface()
{
}
bool Interface::SetCurrentPage(PageHandle *page)
{
	// std::lock_guard<std::mutex> locker(m_mutex);
    if(!page || !page->GetThisPage()){
        LOG_E("null page\n");
        return false;
    }
    previousPage = currentPage;
    currentPage = page->GetThisPage();
	pageQueue.push_front(page);
    LOG_I("current_window=%s,window amount=%d\n", currentPage->Name().c_str(), pageQueue.size());
    return true;
}
bool Interface::CloseCurrentPage()
{
	// std::lock_guard<std::mutex> locker(m_mutex);
    PageHandle *window = nullptr;
    if(pageQueue.size() < 2){
        LOG_W("close current window falied,there is only one page left\n");
        return false;
    }
    window = pageQueue.front();
    window->Release();
	pageQueue.pop_front();
    LOG_I("close window : %s\n", window->GetThisPage()->Name().c_str());
    window->PageClose();
    delete window;
    window = nullptr;
    window = pageQueue.front();
    currentPage = window->GetThisPage();
    return true;
}
void Interface::CloseAllPage()
{
    // ClosePopUpWin();
    PageHandle *window = nullptr;
    // std::lock_guard<std::mutex> locker(m_mutex);
    while (pageQueue.size() > 0)
    {
        window = pageQueue.front();
        window->Release();
        pageQueue.pop_front();
        LOG_I("close window : %s\n", window->GetThisPage()->Name().c_str());
        window->PageClose();
        delete window;
    }
    window = nullptr;
    currentPage = nullptr;
    previousPage = nullptr;
}
void Interface::BackToPage(std::string pageName)
{
    PageHandle *window = nullptr;
    // std::lock_guard<std::mutex> locker(m_mutex);
    while (pageQueue.size() > 1)
    {
        window = pageQueue.front();
        if(pageName == window->GetThisPage()->Name())
        {
            LOG_I("back to %s\n", pageName.c_str());
            break;
        }
        window->Release();
        pageQueue.pop_front();
        LOG_I("close window : %s\n", window->GetThisPage()->Name().c_str());
        window->PageClose();
        delete window;
    }
    window = pageQueue.front();
    currentPage = window->GetThisPage();
}
PageHandle *Interface::GetCurrentWin()
{
	// std::lock_guard<std::mutex> locker(m_mutex);
    PageHandle *page = pageQueue.front();
    return page;
}
PageHandle *Interface::GetCurrentWin(std::string pageName)
{
	// std::lock_guard<std::mutex> locker(m_mutex);
    PageHandle *page = pageQueue.front();
    if(!page){
        LOG_E("page is nullptr, maybe there is no window set\n");
        return nullptr;
    }
    if(pageName == page->GetThisPage()->Name()){
        return page;
    } else {
        LOG_E("current page isn't the target page[current:%s][target:%s]\n", page->GetThisPage()->Name().c_str(), pageName.c_str());
        return nullptr;
    }
}
void Interface::ResetTimeOut()
{
	// std::lock_guard<std::mutex> locker(m_mutex);
    std::list<PageHandle *>::iterator it;
    for(it = pageQueue.begin(); it != pageQueue.end(); it++)
    {
        TimeOut *page = dynamic_cast<TimeOut *>(*it);
        if(page)
        {
            page->ResetTimeOut();
        }
    }
}
PageHandle *Interface::GetWinByName(std::string pageName)
{
	// std::lock_guard<std::mutex> locker(m_mutex);
    std::list<PageHandle *>::iterator it;
    for(it = pageQueue.begin(); it != pageQueue.end(); it++)
    {
        if(pageName == dynamic_cast<PageHandle *>(*it)->GetThisPage()->Name())
        {
            return *it;
        }
    }
    // LOG_W("can't find page(%s)\n", pageNmae.c_str());
    return nullptr;
}